#Region " Imports "

Imports Microsoft.VisualBasic
Imports System.Security
Imports System.Security.Principal
Imports System.Threading
Imports BlazeApps.Library.Page
Imports BlazeApps.Library.Security
Imports BlazeApps.Library.Forum
Imports System.Data

#End Region

Public Class Security
    Inherits WebConfigSettings

#Region " Enums "

    Protected Enum SecurityMask
        Admin
        Editor
        Template
        Reviewer
        Moderator
        Member
        BlogModerator
        EventModerator
        GalleryModerator
        PollModerator
    End Enum

#End Region

#Region " Members "

    Private _CurrentUser As String = HttpContext.Current.User.Identity.Name
    Private _IsUserAuthenticated As Boolean = HttpContext.Current.User.Identity.IsAuthenticated
    Private _AuthenticationType As String = HttpContext.Current.User.Identity.AuthenticationType
    Private _MessageLabel As Label

#End Region

#Region " Properties "

    Public ReadOnly Property CurrentUser() As String
        Get
            Return _CurrentUser
        End Get
    End Property

    Public ReadOnly Property IsUserAuthenticated() As Boolean
        Get
            Return _IsUserAuthenticated
        End Get
    End Property

    Public ReadOnly Property AuthenticationType() As String
        Get
            Return _AuthenticationType
        End Get
    End Property

    Public ReadOnly Property CurrentUserID() As Guid
        Get
            Return Membership.GetUser(_CurrentUser.ToString).ProviderUserKey
        End Get
    End Property

    Public ReadOnly Property CurrentUserID(ByVal UserName As String) As Guid
        Get
            Return Membership.GetUser(UserName).ProviderUserKey
        End Get
    End Property

    Public ReadOnly Property UserName(ByVal UserID As String) As String
        Get
            If String.IsNullOrEmpty(UserID) Then
                Return String.Empty
            Else
                Return Membership.GetUser(New Guid(UserID)).UserName.ToString().Trim()
            End If
        End Get
    End Property

    Public ReadOnly Property UserID(ByVal UserName As String) As Guid
        Get
            Return New Guid(Membership.GetUser(UserName).ProviderUserKey.ToString().Trim())
        End Get
    End Property

    Protected Property MessageLabel() As Label
        Get
            Return _MessageLabel
        End Get
        Set(ByVal value As Label)
            _MessageLabel = value
        End Set
    End Property

#End Region

#Region " Methods "

#Region " Is Allowed Checks "

    Public Function IsAdmin() As Boolean
        Return Roles.IsUserInRole(CurrentUser, "admin")
    End Function

    Public Function IsTemplate() As Boolean
        Return Roles.IsUserInRole(CurrentUser, "template") OrElse IsAdmin()
    End Function

    Public Function IsEditor() As Boolean
        Return Roles.IsUserInRole(CurrentUser, "editor") OrElse IsAdmin()
    End Function

    Public Function IsReviewer() As Boolean
        Return Roles.IsUserInRole(CurrentUser, "reviewer") OrElse IsAdmin() OrElse IsEditor()
    End Function

    Public Function IsModerator() As Boolean
        Return Roles.IsUserInRole(CurrentUser, "moderator") OrElse IsAdmin()
    End Function

    Public Function IsBlogModerator() As Boolean
        Return Roles.IsUserInRole(CurrentUser, "blogmoderator") OrElse IsAdmin()
    End Function

    Public Function IsPollModerator() As Boolean
        Return Roles.IsUserInRole(CurrentUser, "pollmoderator") OrElse IsAdmin()
    End Function

    Public Function IsEventModerator() As Boolean
        Return Roles.IsUserInRole(CurrentUser, "eventmoderator") OrElse IsAdmin()
    End Function

    Public Function IsGalleryModerator() As Boolean
        Return Roles.IsUserInRole(CurrentUser, "gallerymoderator") OrElse IsAdmin()
    End Function

    Public Function IsMember() As Boolean
        Return Roles.IsUserInRole(CurrentUser, "member") OrElse IsAdmin()
    End Function

    Public Function IsGuest() As Boolean
        Return True
    End Function

    Public Function IsBanned() As Boolean
        Return Roles.IsUserInRole(CurrentUser, "banned")
    End Function

    Public Function IsAllowedItemAccess(ByVal ItemID As Guid) As Boolean
        Dim SecurityCheck As New SecurityList
        Dim DT As New DataTable
        Dim DR As DataRow

        If IsAdmin() = True Then
            IsAllowedItemAccess = True
            Exit Function
        Else

            DT = SecurityCheck.GetSecurityList(ItemID)

            If DT.Rows.Count = 0 Then
                IsAllowedItemAccess = True
                Exit Function
            End If

            If DT.Rows.Count > 0 Then
                Dim InRole As Boolean = False

                If IsUserAuthenticated = False Then
                    InRole = False
                Else
                    For Each DR In DT.Rows
                        InRole = Roles.IsUserInRole(CurrentUser, DR("rolename").ToString.Trim)
                    Next
                End If

                If InRole = True Then
                    IsAllowedItemAccess = True
                    Exit Function
                Else
                    IsAllowedItemAccess = False
                    Me.MessageLabel.Text = Resources.Resource.SecurityNoPermissions.ToString.Trim
                    Exit Function
                End If

            End If
        End If

    End Function

#End Region

#Region " Show Controls/Items Checks "

    Protected Function ShowControls(ByVal RoleType As SecurityMask, ByVal DisplayMessage As Boolean) As Boolean

        If IsUserAuthenticated = False Then
            ShowControls = False
            If DisplayMessage Then
                Me.MessageLabel.Text = Resources.Resource.SecurityLoginRequired.ToString
            End If
        Else

            Select Case RoleType
                Case SecurityMask.Admin
                    If IsAdmin() = True Then
                        ShowControls = True
                        Me.MessageLabel.Text = ""
                        Exit Function
                    Else
                        ShowControls = False
                        If DisplayMessage Then
                            Me.MessageLabel.Text = Resources.Resource.SecurityNotAdmin.ToString
                        End If
                        Exit Function
                    End If
                Case SecurityMask.Editor
                    If IsEditor() = True Then
                        ShowControls = True
                        Me.MessageLabel.Text = ""
                        Exit Function
                    Else
                        ShowControls = False
                        If DisplayMessage Then
                            Me.MessageLabel.Text = Resources.Resource.SecurityNotEditor.ToString
                        End If
                        Exit Function
                    End If
                Case SecurityMask.Moderator
                    If IsModerator() = True Then
                        ShowControls = True
                        Me.MessageLabel.Text = ""
                        Exit Function
                    Else
                        ShowControls = False
                        If DisplayMessage Then
                            Me.MessageLabel.Text = Resources.Resource.SecurityNotModerator.ToString
                        End If
                        Exit Function
                    End If
                Case SecurityMask.BlogModerator
                    If IsModerator() = True Then
                        ShowControls = True
                        Me.MessageLabel.Text = ""
                        Exit Function
                    Else
                        ShowControls = False
                        If DisplayMessage Then
                            Me.MessageLabel.Text = Resources.Resource.SecurityNotModerator.ToString
                        End If
                        Exit Function
                    End If
                Case SecurityMask.EventModerator
                    If IsModerator() = True Then
                        ShowControls = True
                        Me.MessageLabel.Text = ""
                        Exit Function
                    Else
                        ShowControls = False
                        If DisplayMessage Then
                            Me.MessageLabel.Text = Resources.Resource.SecurityNotModerator.ToString
                        End If
                        Exit Function
                    End If
                Case SecurityMask.GalleryModerator
                    If IsModerator() = True Then
                        ShowControls = True
                        Me.MessageLabel.Text = ""
                        Exit Function
                    Else
                        ShowControls = False
                        If DisplayMessage Then
                            Me.MessageLabel.Text = Resources.Resource.SecurityNotModerator.ToString
                        End If
                        Exit Function
                    End If
                Case SecurityMask.PollModerator
                    If IsModerator() = True Then
                        ShowControls = True
                        Me.MessageLabel.Text = ""
                        Exit Function
                    Else
                        ShowControls = False
                        If DisplayMessage Then
                            Me.MessageLabel.Text = Resources.Resource.SecurityNotModerator.ToString
                        End If
                        Exit Function
                    End If
                Case SecurityMask.Template
                    If IsTemplate() = True Then
                        ShowControls = True
                        Me.MessageLabel.Text = ""
                        Exit Function
                    Else
                        ShowControls = False
                        If DisplayMessage Then
                            Me.MessageLabel.Text = Resources.Resource.SecurityNotTemplate.ToString
                        End If                    
                        Exit Function
                    End If
                Case SecurityMask.Reviewer
                    If IsReviewer() = True Then
                        ShowControls = True
                        Me.MessageLabel.Text = ""
                        Exit Function
                    Else
                        ShowControls = False
                        If DisplayMessage Then
                            Me.MessageLabel.Text = Resources.Resource.SecurityNotReviewer.ToString
                        End If
                        Exit Function
                    End If
                Case SecurityMask.Member
                    If IsMember() = True Then
                        ShowControls = True
                        Me.MessageLabel.Text = ""
                        Exit Function
                    Else
                        ShowControls = False
                        If DisplayMessage Then
                            Me.MessageLabel.Text = Resources.Resource.SecurityNotMember.ToString
                        End If
                        Exit Function
                    End If
                Case Else
            End Select

        End If

    End Function

    Public Function ShowItem(ByVal ItemID As Guid) As Boolean
        Dim SecurityCheck As New SecurityList
        Dim DT As New DataTable
        Dim DR As DataRow

        If IsAdmin() = True Then
            Return True
        Else

            DT = SecurityCheck.GetSecurityList(ItemID)

            If DT.Rows.Count = 0 Then
                Return True
            End If

            If DT.Rows.Count > 0 Then
                Dim InRole As Boolean = False

                If IsUserAuthenticated = False Then
                    InRole = False
                Else
                    For Each DR In DT.Rows
                        InRole = Roles.IsUserInRole(CurrentUser, DR("rolename").ToString.Trim)
                    Next
                End If

                If InRole = True Then
                    Return True
                Else
                    Return False
                End If

            End If
        End If

    End Function

    Public Function ShowItem(ByVal ItemID As Guid, ByVal ItemType As BlazeApps.Library.Base.ItemType) As Boolean
        Dim SecurityCheck As New SecurityList
        Dim DT As New DataTable
        Dim DR As DataRow

        If IsAdmin() = True Then
            Return True
        Else

            If ItemType = BlazeApps.Library.Base.ItemType.Forums Then
                Dim topic As Topic = topic.GetTopic(ItemID)

                DT = SecurityCheck.GetSecurityList(topic.ForumID)
            Else
                DT = SecurityCheck.GetSecurityList(ItemID)
            End If

            If DT.Rows.Count = 0 Then
                Return True
            End If

            If DT.Rows.Count > 0 Then
                Dim InRole As Boolean = False

                If IsUserAuthenticated = False Then
                    InRole = False
                Else
                    For Each DR In DT.Rows
                        InRole = Roles.IsUserInRole(CurrentUser, DR("rolename").ToString.Trim)
                    Next
                End If

                If InRole = True Then
                    Return True
                Else
                    Return False
                End If

            End If
        End If

    End Function

#End Region

#Region " Checkin/Checkout Items "

    Protected Sub CheckIn(ByVal PageID As Guid, ByVal UserID As Guid)
        PageLock.DeletePageLock(PageID, UserID)
    End Sub

    Protected Sub CheckOut(ByVal PageID As Guid, ByVal UserID As Guid)
        Dim Item As PageLock = PageLock.GetPageLock(PageID)

        Item.pageid = PageID
        Item.currentuserid = UserID
        Item.Save()
    End Sub

    Protected Function IsCheckedOut(ByVal PageID As Guid) As Boolean
        Dim Item As PageLock = PageLock.GetPageLock(PageID)
        Dim Check As Boolean

        If Item.currentuserid.Equals(Guid.Empty) Then
            Check = False
        ElseIf Not Item.currentuserid.Equals(Guid.Empty) Then
            Check = True
        End If

        Return Check

    End Function

    Protected Function IsCheckedOutByCurrentUser(ByVal PageID As Guid, ByVal UserID As Guid) As Boolean
        Dim Item As PageLock = PageLock.GetPageLock(PageID)
        Dim Check As Boolean

        If Item.currentuserid.Equals(System.DBNull.Value) Then
            Check = False
        ElseIf Item.currentuserid = UserID Then
            Check = True
        End If

        Return Check

    End Function

#End Region

#End Region

End Class
